<template>
  <div class="article-container">
    <van-nav-bar
      class="app-nav-bar"
      title="文章详情"
      left-arrow
      @click-left="$router.back()" />
    <div class="article-wrap">
      <h1 class="title">{{ article.title }}</h1>
      <van-cell center class="user-info">
        <div slot="title" class="name">{{ author.nickname }}({{article.name}})</div>
        <van-image
          slot="icon"
          class="avatar"
          round
          fit="cover"
          :src="author.imgUrl"
        />
        <div slot="label" class="pubdate">
          {{ article.createDate | formattingTime }}
        </div>
        <van-button
          v-if="article.name === user.username"
          class="follow-btn"
          type="danger"
          icon="delete"
          round
          size="mini"
          @click="onDelectedArticle" />
      </van-cell>
      <div
        class="markdown-body"
        v-html="article.content"
        ref="article-content"
      ></div>
      <van-image
        v-if="article.uploader"
        width="100%"
        height="50%"
        :src="article.uploader"
      />
      <comment-list
        :articleId="articleId"
        :list="commentList"
      />
    </div>
    <div class="article-bottom">
      <van-button
        class="comment-btn"
        type="default"
        round
        icon="records"
        size="small"
        @click="isPostShow = true"
      >写评论</van-button>
      <van-icon
        :color="article.isCollected ? 'orange' : '#777'"
        :name="article.isCollected ? 'star' : 'star-o'"
        @click="onCollect"
      />
    </div>
    <van-popup
      v-model="isPostShow"
      position="bottom"
    >
      <post-comment
        :articleId="articleId"
        @post-success="onPostSuccess"
      />
    </van-popup>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import './github-markdown.css'
import CommentList from './components/comment-list'
import PostComment from './components/post-comment'
export default {
  components: {
    CommentList,
    PostComment
  },
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      author: {},
      article: {},
      isPostShow: false,
      commentList: []
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {
    articleId () {
      this.loadArticle()
    }
  },
  created () {
    this.loadArticle()
  },
  mounted () {},
  methods: {
    async loadArticle () {
      await this.$http(`api/tableTest/${this.articleId}`).then(({ data: res }) => {
        if (res.code === 0) {
          this.article = res.data
          this.commentList = res.data.comment.reverse()
          this.loadAuthorApi(res.data.name).then(res => {
            this.author = res.data
            this.author.imgUrl = process.env.VUE_APP_API_BASE_URL + res.data.photo
          })
          this.loadAuthorApi(this.user.username).then(res => {
            const item = res.data.collectList.find(item => { return item === this.articleId })
            if (item) {
              this.article.isCollected = true
            } else {
              this.article.isCollected = false
            }
            this.$forceUpdate()
          })
        }
      })
    },
    async onCollect () {
      this.$toast.loading({
        message: '操作中...',
        forbidClick: true
      })
      const data = {
        username: this.user.username,
        collect: this.articleId
      }
      this.$http.put('api/user/collect', data).then(({ data: res }) => {
        this.article.isCollected = !this.article.isCollected
        const personalDetails = { ...this.user }
        const item = personalDetails.collectList.find(item => { return item === this.articleId })
        if (item) {
          personalDetails.collectList = personalDetails.collectList.filter(item => { return item !== this.articleId })
        } else {
          personalDetails.collectList.push(this.articleId)
        }
        this.$store.commit('setUser', personalDetails)
        this.$toast.success(`${this.article.isCollected ? '' : '取消'}收藏成功`)
        this.$forceUpdate()
      })
    },
    loadAuthorApi (user) {
      return new Promise(resolve => {
        this.$http(`api/user/logoInfo/${user}`).then(({ data: res }) => {
          if (res.code === 0) {
            resolve(res)
          }
        })
      })
    },
    onDelectedArticle () {
      return new Promise(resolve => {
        this.$http.delete('api/tableTest', {
          data: [this.articleId]
        }).then(({ data: res }) => {
          if (res.code === 0) this.$router.push({ name: 'home', params: { refresh: true } })
        })
      })
    },
    onPostSuccess (comment) {
      this.commentList.unshift(comment)
      this.totalCommentCount++
      this.isPostShow = false
    }
  }
}
</script>

<style scoped lang="less">
.article-wrap {
  position: fixed;
  left: 0;
  right: 0;
  top: 46px;
  bottom: 44px;
  overflow-y: auto;
}
.title {
  font-size: 20px;
  color: #3a3a3a;
  padding: 24px 20px 18px;
  background-color: #fff;
  margin: 0;
}

.user-info {
  .avatar {
    width: 35px;
    height: 35px;
    margin-right: 8px;
  }
  .name {
    font-size: 12px;
    color: #333333;
  }
  .pubdate {
    font-size: 11px;
    color: #b4b4b4;
  }
  .follow-btn {
    width: 44px;
    height: 18px;
  }
}

ul {
  list-style: unset;
}

.markdown-body {
  padding: 14px;
  background-color: #fff;
}

.article-bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  box-sizing: border-box;
  height: 44px;
  border-top: 1px solid #d8d8d8;
  background-color: #fff;
  .comment-btn {
    width: 160px;
    height: 30px;
    border: 1px solid #eeeeee;
    font-size: 15px;
    line-height: 23px;
    color: #a7a7a7;
  }
  .van-icon {
    font-size: 20px;
    .van-info {
      font-size: 11px;
      background-color: #e22829;
    }
  }
}
</style>
